import React, { useState, useRef } from 'react';
import './searchBox.css';

// 定义下拉选项类型
type DropdownOption = {
  label: string;
  value: string;
};

// 定义组件 props 类型
interface SearchBoxProps {
 
  placeholder?: string;
  onSearch: (query: string, selectedOption: string) => void;
}

const SearchBox: React.FC<SearchBoxProps> = ({  placeholder = '搜索相关专题', onSearch }) => {
  // 使用组件时传入以下选项
const searchOptions: DropdownOption[] = [
  { label: '设计模板', value: 'design_template' },
  { label: '视频', value: 'video' },
  { label: '音频', value: 'audio' },
  { label: '图片', value: 'image' },
  { label: '设计素材', value: 'design_material' },
  { label: '办公文档', value: 'document' },
];
  const [searchQuery, setSearchQuery] = useState('');
  const [selectedOption, setSelectedOption] = useState(searchOptions[0].value);
  const [isDropdownVisible, setIsDropdownVisible] = useState(false);
  const dropdownRef = useRef<HTMLDivElement>(null);

  // 点击外部关闭下拉
  React.useEffect(() => {
    const handleClickOutside = (event: MouseEvent) => {
      if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
        setIsDropdownVisible(false);
      }
    };
    if (isDropdownVisible) {
      document.addEventListener('mousedown', handleClickOutside);
    } else {
      document.removeEventListener('mousedown', handleClickOutside);
    }
    return () => document.removeEventListener('mousedown', handleClickOutside);
  }, [isDropdownVisible]);

  const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    setSearchQuery(e.target.value);
  };

  const handleSearch = () => {
    onSearch(searchQuery, selectedOption);
  };

  const handleOptionClick = (option: DropdownOption) => {
    setSelectedOption(option.value);
    setIsDropdownVisible(false);
  };

  const selectedLabel = searchOptions.find(opt => opt.value === selectedOption)?.label ;

  return (
    <div className="searchbox-outer">
      <div className="search-box-container">
        <div className="dropdown-container" ref={dropdownRef} onClick={() => setIsDropdownVisible(v => !v)}>
          <span className="dropdown-label">{selectedLabel}</span>
          <svg className="dropdown-icon" viewBox="0 0 24 24">
            <path d="M7 10l5 5 5-5z"></path>
          </svg>
          {isDropdownVisible && (
            <ul className="dropdown-menu">
              {searchOptions.map((option) => (
                <li key={option.value} onClick={() => handleOptionClick(option)} className={option.value === selectedOption ? 'active' : ''}>
                  {option.label}
                </li>
              ))}
            </ul>
          )}
        </div>
        <div className="searchbox-divider" />
        <input
          className="search-box-input"
          type="text"
          placeholder={placeholder}
          value={searchQuery}
          onChange={handleInputChange}
        />
        <button className="search-box-button" onClick={handleSearch}>
          <svg className="search-icon" viewBox="0 0 24 24">
            <path d="M16.32 14.9l5.39 5.4a1 1 0 0 1-1.42 1.4l-5.38-5.38a8 8 0 1 1 1.41-1.41zM10 16a6 6 0 1 0 0-12 6 6 0 0 0 0 12z"></path>
          </svg>
          搜索
        </button>
      </div>
    </div>
  );
};

export default SearchBox;